package com.twu.biblioteca.services;

import com.twu.biblioteca.BibliotecaApp;
import com.twu.biblioteca.Session;
import com.twu.biblioteca.models.User;
import com.twu.biblioteca.resources.UserList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * User: arvind
 * Date: 11/10/13
 * Time: 4:30 PM
 * To change this template use File | Settings | File Templates.
 */
public class LoginService {

    public String initiateLogin(){

        Scanner getLoginDetails=new Scanner(System.in);
        System.out.print("Enter username : ");
        String username=getLoginDetails.next();

        System.out.print("Enter password : ");
        String password=getLoginDetails.next();

        if(authenticateLogin(username, password))
        {
            Session session=BibliotecaApp.getSession();
            session.setLoginStatus(true);
            session.setUser(UserList.getInstance().getUser(username));
            return "Login Successful.";
        }
        else
        {
            return getLoginErrorMessage();
        }
    }

    public static boolean authenticateLogin(String username,String password) {

        UserList userListInstance= UserList.getInstance();

        User user=userListInstance.getUser(username);
        if(user!=null)
        {
            String originalPassword=user.getPassword();

            if(password.equals(originalPassword))
                return true;
            else
                return false;
        }
        else
        {
            return false;
        }
    }

    private String getLoginErrorMessage() {
        return "The Username and password you entered were incorrect.";
    }

    public static String getLoginMessage() {

        return "Please Login to use the feature.";
    }
}

